package com.codingman.struts2demo4.crud;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class EmployeeAction extends ActionSupport implements ModelDriven<Employee>, Preparable {
	
	private Employee employee = new Employee();
	private List<Employee> employees;
	
	private List<String> countryList = new ArrayList<String>();
	private Map<String, String> sexList = new HashMap<String, String>();
	
	public List<String> getCountryList() {
		return countryList;
	}

	public void setCountryList(List<String> countryList) {
		this.countryList = countryList;
	}

	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	@Override
	public Employee getModel() {
		return employee;
	}

	public String save() {
		EmployeeDao.save(employee);
		return SUCCESS;
	}

	public String delete() {
		EmployeeDao.delete(employee.getId());
		return SUCCESS;
	}

	public String editform() {
		EmployeeDao.getEmployeeById(employee);
		return SUCCESS;
	}
	
	public String update() {
		EmployeeDao.update(employee);
		return SUCCESS;
	}
	
	public String list() {
		employees = EmployeeDao.getAllEmployees();
		return SUCCESS;
	}

	public List<Employee> getEmployees() {
		return employees;
	}

	public void setEmployees(List<Employee> employees) {
		this.employees = employees;
	}
	
	@Override
	public void prepare() throws Exception {
		initCountryList();
		initSexList();
	}

	private void initSexList() {
		sexList.put("M", "Male");
		sexList.put("F", "Female");
	}

	private void initCountryList() {
		countryList.add("China");
		countryList.add("USA");
		countryList.add("German");
		countryList.add("England");
	}

	public Map<String, String> getSexList() {
		return sexList;
	}

	public void setSexList(Map<String, String> sexList) {
		this.sexList = sexList;
	}
}
